package com.example.zhunongzonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhunongzonghe.beans.ZnExchangeCommentBean;
import com.example.zhunongzonghe.dao.ZnExchangeCommentMapper;
import com.example.zhunongzonghe.dao.ZnExchangePostMapper;
import com.example.zhunongzonghe.dao.ZnUsersMapper;
import com.example.zhunongzonghe.entity.ZnExchangeComment;
import com.example.zhunongzonghe.entity.ZnExchangePost;
import com.example.zhunongzonghe.entity.ZnUsers;
import com.example.zhunongzonghe.service.IZnExchangeCommentService;
import com.example.zhunongzonghe.service.IZnUsersService;
import com.example.zhunongzonghe.utils.BeanCopyUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class ZnExchangeCommentServiceImpl extends ServiceImpl<ZnExchangeCommentMapper, ZnExchangeComment> implements IZnExchangeCommentService {

    @Autowired
    private ZnExchangeCommentMapper znExchangeCommentMapper;

    @Autowired
    private ZnUsersMapper znUsersMapper;
    @Autowired
    private IZnUsersService znUsersService;
    @Autowired
    private ZnExchangePostMapper znExchangePostMapper;
    /**
     * 分页查询全部评论信息以及分页搜索
     * @param currentPage
     * @param pageNum
     * @param SearchInfo
     * @return
     */
    @Override
    public Page<ZnExchangeCommentBean> CommentAll(Integer currentPage, Integer pageNum, Object SearchInfo) {
        Page<ZnExchangeCommentBean> page = new Page<>(currentPage,pageNum);
        MPJLambdaWrapper<ZnExchangeComment> wrapper = new MPJLambdaWrapper<ZnExchangeComment>()
                .selectAll(ZnExchangeComment.class)
                .select(ZnExchangePost::getPostName)
                .select(ZnUsers::getNickname)
                .like(ZnExchangeComment::getCommentContent,SearchInfo)
                .or().like(ZnUsers::getNickname,SearchInfo)
                .or().like(ZnExchangePost::getPostName,SearchInfo)
                .innerJoin(ZnUsers.class,ZnUsers::getId,ZnExchangeComment::getUserId)
                .innerJoin(ZnExchangePost.class,ZnExchangePost::getPostId,ZnExchangeComment::getPostId);
        Page<ZnExchangeCommentBean> beanPage = znExchangeCommentMapper.selectJoinPage(page,ZnExchangeCommentBean.class,wrapper);
        return beanPage;
    }

    /**
     * 分页查询评论
     * @param postId
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<ZnExchangeCommentBean> CommentList(Integer postId , Integer currentPage, Integer pageSize) {
        Page<ZnExchangeComment> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<ZnExchangeComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangeComment::getPostId,postId);
        queryWrapper.eq(ZnExchangeComment::getRootId,-1);
        Page<ZnExchangeComment> commentPage = znExchangeCommentMapper.selectPage(page,queryWrapper);
        List<ZnExchangeComment> commentList = commentPage.getRecords().stream()
                .sorted(Comparator.comparing(ZnExchangeComment::getCreateTime).reversed())
                .collect(Collectors.toList());
        List<ZnExchangeCommentBean> commentBeanList =getToCommentList(commentList);
        LambdaQueryWrapper<ZnExchangeComment> commentator = new LambdaQueryWrapper<>();
        commentator.eq(ZnExchangeComment::getPostId,postId);
        Long comment = znExchangeCommentMapper.selectCount(commentator);
        for (ZnExchangeCommentBean commentBean: commentBeanList
             ) {
            List<ZnExchangeCommentBean> beanList = getChildren(commentBean.getCommentId(),commentBean.getPostId());
            commentBean.setChildren(beanList);
            commentBean.setTotalComments(comment);
        }
        Page<ZnExchangeCommentBean> beanPage = new Page<>(currentPage,pageSize,page.getTotal());
        beanPage.setRecords(commentBeanList);
        return beanPage;
    }
    //根据根评论的id，来查询对应的所有子评论
    private List<ZnExchangeCommentBean> getChildren(Integer id,Integer postId){
        LambdaQueryWrapper<ZnExchangeComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangeComment::getToCommentId,id);
        queryWrapper.eq(ZnExchangeComment::getPostId,postId);
//        queryWrapper.orderByDesc(ZnExchangeComment::getCreateTime);//对子评论按照时间进行排序
        List<ZnExchangeComment> commentList = znExchangeCommentMapper.selectList(queryWrapper);
//        List<ZnExchangeComment> streamList = commentList.stream()
//                .sorted(Comparator.comparing(ZnExchangeComment::getCreateTime).reversed())
//                .collect(Collectors.toList());
        List<ZnExchangeCommentBean> commentBeans = getToCommentList(commentList);
        return commentBeans;
    }

    /**
     * 封装返回响应
     * @param commentList
     * @return
     */
    private List<ZnExchangeCommentBean> getToCommentList(List<ZnExchangeComment> commentList){
        List<ZnExchangeCommentBean> commentBeanList =
                BeanCopyUtils.copyBeanList(commentList,ZnExchangeCommentBean.class);
        for (ZnExchangeCommentBean commentBean: commentBeanList
        ) {
            String nickName = znUsersMapper.selectById(commentBean.getUserId()).getNickname();
            String userAvatar = znUsersMapper.selectById(commentBean.getUserId()).getAvatar();
            commentBean.setNickname(nickName);
            commentBean.setUserAvatar(userAvatar);
            if (commentBean.getToCommentUserid() != -1){
                String toCommentNickName = znUsersMapper.selectById(commentBean.getToCommentUserid()).getNickname();
                String toCommentUserAvatar = znUsersMapper.selectById(commentBean.getUserId()).getAvatar();
                commentBean.setToCommentNickName(toCommentNickName);
                commentBean.setUserAvatar(toCommentUserAvatar);
            }
        }

        return commentBeanList;
    }
    /**
     * 用户发布根评论
     * @param commentBean
     * @param userName
     * @return
     */
    @Override
    public ZnExchangeComment AddRootComment(ZnExchangeCommentBean commentBean, String userName) {
        ZnUsers znUsers = znUsersService.getUserName(userName);
        ZnExchangeComment comment = new ZnExchangeComment();
        comment.setCommentContent(commentBean.getCommentContent());
        comment.setPostId(commentBean.getPostId());
        comment.setUserId(znUsers.getId());
        comment.setRootId(-1);
        comment.setToCommentId(-1);
        comment.setToCommentUserid(-1);
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        znExchangeCommentMapper.insert(comment);
        return comment;
    }

    /**
     * 用户发布子评论
     * @param commentBean
     * @param userName
     * @return
     */
    @Override
    public ZnExchangeComment AddSonComment(ZnExchangeCommentBean commentBean, String userName) {
        ZnUsers znUsers = znUsersService.getUserName(userName);
        ZnExchangeComment znExchangeComment = new ZnExchangeComment();
        znExchangeComment.setToCommentUserid(commentBean.getUserId());
        znExchangeComment.setCommentContent(commentBean.getCommentContent());
        znExchangeComment.setRootId(1);
        znExchangeComment.setToCommentId(commentBean.getCommentId());
        znExchangeComment.setUserId(znUsers.getId());
        znExchangeComment.setPostId(commentBean.getPostId());
        znExchangeComment.setCreateTime(LocalDateTime.now());
        znExchangeComment.setUpdateTime(LocalDateTime.now());
        znExchangeCommentMapper.insert(znExchangeComment);
        return znExchangeComment;
    }

    @Override
    public Long CommentNumBer(Integer postId) {

        LambdaQueryWrapper<ZnExchangeComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnExchangeComment::getPostId,postId);
        Long num = znExchangeCommentMapper.selectCount(queryWrapper);
        return num;
    }


}
